package com.example.gesturerecognition;

import androidx.appcompat.app.AppCompatActivity;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener {
    private GestureLibrary library;
    private boolean loadStatus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //加载手势文件
        library= GestureLibraries.fromRawResource(this,R.raw.mygestures);
        GestureOverlayView gesture=(GestureOverlayView) findViewById(R.id.gestures);
        gesture.addOnGesturePerformedListener(this);
    }

    @Override
    public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
        loadStatus=library.load();
        if(loadStatus){
            ArrayList<Prediction> pres=library.recognize(gesture);
            if(!pres.isEmpty()){
                Prediction pre=pres.get(0);
                if(pre.score>4){
                    Toast.makeText(this, pre.name, Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(this, "手势匹配不成功", Toast.LENGTH_LONG).show();
                }
            }else{
                Toast.makeText(this, "手势库加载失败", Toast.LENGTH_LONG).show();
            }
        }
    }
}
